package com.szqhyxj.youxiangju.biz.partnerHome.equipment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BasePromptFragment;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.partnerHome.equipment.bean.BrandPOListItem;
import com.szqhyxj.youxiangju.biz.partnerHome.equipment.bean.StockBrand;
import com.szqhyxj.youxiangju.biz.partnerHome.equipment.subordinateList.SubordinateInfoActivity;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.picker.OptionPicker;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.util.PickerUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import me.jessyan.autosize.internal.CustomAdapt;

/**
 * 设备管理
 * Created by leung on 2018/10/31.
 */

public class PartnerEquipmentFragment extends BasePromptFragment implements PartnerEquipmentContract.View,CustomAdapt{

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.record)
    TextView record;
    @BindView(R.id.amount)
    TextView amount;
    @BindView(R.id.icon)
    ImageView icon;
    @BindView(R.id.brand)
    TextView brand;
    @BindView(R.id.stock_amount)
    TextView stockAmount;
    @BindView(R.id.number_list)
    ListView numberList;
    Unbinder unbinder;
    @BindView(R.id.equipment_details)
    LinearLayout equipmentDetails;
    @BindView(R.id.equipment_number_title)
    TextView equipmentNumberTitle;
    @BindView(R.id.lack)
    LinearLayout lack;

    private PartnerEquipmentPresenter presenter;
    private StockBrand stockBrand;
    private NumberAdapter adapter;
    private List<String> devices;


    public static PartnerEquipmentFragment newInstance(){
        Bundle args = new Bundle();
        PartnerEquipmentFragment fragment = new PartnerEquipmentFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void start(){
        devices = new ArrayList<>();
        adapter = new NumberAdapter(devices);
        numberList.setAdapter(adapter);
        presenter = new PartnerEquipmentPresenter(this);
        presenter.initStockBrand();
        initEvent();
    }

    private void initEvent(){
        numberList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
                SubordinateInfoActivity.start(getActivity(),brand.getText().toString(),devices.get(i));
            }
        });
    }

    @Override
    public void onResume(){
        super.onResume();
        start();
    }

    @Override
    protected int getLayoutId(){
        return R.layout.fragment_partner_performance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView(){
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void showStockBrand(StockBrand stockBrand){
        this.stockBrand = stockBrand;
        equipmentDetails.setVisibility(View.VISIBLE);
        numberList.setVisibility(View.VISIBLE);
        equipmentNumberTitle.setVisibility(View.VISIBLE);
        amount.setText(stockBrand.getInventery());
        //默认显示第一种机型
        BrandPOListItem item = stockBrand.getBrandPOList().get(0);
        showBrand(item);
        presenter.getStockList(item.getBrandId());
    }

    @Override
    public void showStockList(List<String> brandNames){
        devices.clear();
        devices.addAll(brandNames);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showNone(){
        lack.setVisibility(View.VISIBLE);
        numberList.setVisibility(View.GONE);
        amount.setText("0");
    }

    @Override
    public void showPickerStock(String[] brandNames){
        PickerUtil.onOptionPicker(getActivity(), brandNames, new OptionPicker.OnOptionPickListener(){
            @Override
            public void onOptionPicked(int index, String item){
                BrandPOListItem brandPOListItem = stockBrand.getBrandPOList().get(index);
                presenter.getStockList(brandPOListItem.getBrandId());
                showBrand(brandPOListItem);
            }
        });
    }

    @Override
    public void showBrand(BrandPOListItem item){
        LoadImageUtils.loadImage(icon, item.getFile());
        brand.setText(item.getBrandName());
        stockAmount.setText(item.getNumber());
    }

    @OnClick(R.id.equipment_details)
    public void onViewClicked(View view){
        switch(view.getId()){
            case R.id.equipment_details:
                presenter.setBrandName();
                break;
        }
    }

    @Override
    public boolean isBaseOnWidth(){
        return true;
    }

    @Override
    public float getSizeInDp(){
        return 360;
    }
}